from django.db import models
from django.contrib.auth.models import User
import logging
from datetime import datetime
from swingtime import models as swingtime

class TemporalBase(models.Model):
  created_at = models.DateTimeField("created at")
  updated_at = models.DateTimeField("updated at")
  
  class Meta:
    abstract = True
    
  def save(self, *args, **kwargs):
    if self.created_at == None:
      self.created_at = datetime.now()
    self.updated_at = datetime.now()
    super(TemporalBase, self).save(*args, **kwargs)
  
  def __repr__(self):
    return self.__str__()
  
class EventType(models.Model):
  abbr = models.CharField(max_length=10)
  label = models.CharField(max_length=100)

class Event(TemporalBase):
  title = models.CharField(max_length=30)
  description = models.CharField(max_length=100)
  event_type = models.ForeignKey('EventType')
  start_time = models.DateTimeField("start time")
  end_time = models.DateTimeField("end time")


class School(TemporalBase):
  name = models.CharField(max_length=50)
  
  def __unicode__(self): 
    return self.name 

class Course(TemporalBase):
  name = models.CharField(max_length=50, unique=True)
  course_id = models.CharField(max_length=10)
  quarter = models.CharField(max_length=50)
  
  class Meta:
    ordering = ('name',)
  
  def __eq__(self, other):
    return (self.course_id.lower() == 
            other.course_id.lower()) 

  def to_json(self):
    return str({'name':str(self.name), 'id': self.id})
  
  def __unicode__(self): 
    return self.name 
  
class Department(TemporalBase):
  name = models.CharField(max_length=20)
  school = models.ForeignKey('School')
  department_id = models.CharField(max_length=10)
  courses = models.ManyToManyField(Course)
  
  def __str__(self):
    return "%s (%s)" % (self.name, self.department_id)
  
  def __unicode__(self): 
    return self.name 

class Location(TemporalBase):
  name = models.CharField(max_length=50)
  school = models.ForeignKey(School)
  
  def __unicode__(self): 
    return self.name 

class UserProfile(User, TemporalBase):
  courses = models.ManyToManyField(Course)
  school = models.ForeignKey(School)
  is_teacher = models.BooleanField()
  lessons = models.ManyToManyField('Lesson', through='LessonsContributors')
  
  def __str__(self): 
    return "name:%s" % \
      (self.first_name) 

  def __unicode__(self): 
    return self.first_name
  
class CourseParticipants(TemporalBase):
  is_teacher = models.BooleanField()
  user = models.ForeignKey(UserProfile)
  course = models.ForeignKey('Course')
  
class LessonsContributors(TemporalBase):
  is_teacher = models.BooleanField()
  user = models.ForeignKey(UserProfile)
  lesson = models.ForeignKey('Lesson')
  
class Lesson(Event):
  name = models.CharField(max_length=50)
  course = models.ForeignKey(Course)
  capacity = models.PositiveIntegerField()
  occupancy = models.PositiveIntegerField()
  contributors = models.ManyToManyField(UserProfile, through='LessonsContributors')
  location = models.ForeignKey(Location)
  
  def __unicode__(self): 
    return self.name 
